package com.web.frame.user;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class NewHtmlFile {
    Logger logger = LoggerFactory.getLogger(NewHtmlFile.class);
    public void newFile(String name){
        try {
            String filePath = this.getClass().getClassLoader().getResource("static/html/indexPage.html").getPath();
            String createPath = "C:\\WebFrameServer\\files\\user";

            String templateContent = "";
            FileInputStream fileInputStream = new FileInputStream(filePath);
            int length = fileInputStream.available();
            byte bytes[] = new byte[length];
            fileInputStream.read(bytes);
            fileInputStream.close();

            templateContent = new String(bytes);
            templateContent = templateContent.replaceAll("##userName##", name);

            String fileName = name + ".html";
            fileName = createPath + "/" + fileName;
            FileOutputStream fileOutputStream = new FileOutputStream(fileName);

            byte tagBytes[] = templateContent.getBytes();
            fileOutputStream.write(tagBytes);
            fileOutputStream.close();
        }catch (Exception e) {
            e.printStackTrace();
            logger.error("错误代码:" + e.getMessage());
        }
    }
}
